package com.jie.myvhr.service;


import com.jie.myvhr.bean.Hr;
import com.jie.myvhr.bean.MsgContent;
import com.jie.myvhr.bean.SysMsg;
import com.jie.myvhr.common.HrUtils;
import com.jie.myvhr.mapper.SysMsgMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author jie
 * @date 2019/6/29 -15:57
 */
@Service
@Transactional
public class SysMsgService {
    @Resource
    private SysMsgMapper sysMsgMapper;
    @Resource
    private HrService hrService;

    public List<SysMsg> getSysMsgByPage(Integer page, Integer size) {
        int start = (page - 1) * size;
        return sysMsgMapper.getSysMsg(start, size, HrUtils.getCurrentHr().getId());
    }

    /**
     * 消息发送需要往内容表插入一条内容
     * 然后给每个人都发送一条信息
     */
    @PreAuthorize("hasRole('ROLE_admin')")//只有管理员可以发送系统消息
    public boolean sendMsg(MsgContent msg) {
        int result = sysMsgMapper.sendMsg(msg);
        List<Hr> allHr = hrService.getAllHr();
        //这里插入了5条数据（给每个人都发送了一条数据）
        int result2 = sysMsgMapper.addMsg2AllHr(allHr, msg.getId());
        return result2==allHr.size();
    }

    public boolean markRead(Long flag){
        if (flag!=-1){
            return sysMsgMapper.markRead(flag, HrUtils.getCurrentHr().getId())==1;
        }
        sysMsgMapper.markRead(flag,HrUtils.getCurrentHr().getId());
        return true;
    }
}
